#!/usr/bin/env/python3
# -*- coding: utf-8 -*-

"""
@Time    : 2020/3/23 11:14
@Author  : Chen Liu
@FileName: binary_sum.py
@Software: PyCharm
"""


class Solution:
    def addBinary(self, a: str, b: str) -> str:
        a_l = len(a)
        b_l = len(b)

        if a_l > b_l:
            b = '0' * (a_l - b_l) + b
        if a_l < b_l:
            a = '0' * (b_l - a_l) + a

        print("a: ", a)
        print("b: ", b)
        res = ''
        last = 0
        length = max(a_l, b_l)
        for i in range(length-1, -1, -1):
            print(i)
            if a[i] == b[i] and a[i] == '1':
                res += str(last)
                last = 1
            elif a[i] == b[i] and a[i] == '0':
                res += str(last)
                last = 0
            else:
                if last + 1 == 2:
                    res += str(0)
                    last = 1
                else:
                    res += str(1)
                    last = 0
        if last == 1:
            res += str(1)

        return res[::-1]

    def add(self, a, b):
        a = int(a, 2)
        b = int(b, 2)

        return bin(a + b)[2:]


if __name__ == "__main__":
    s = Solution()
    a = '11'
    b = '1'
    # res = s.addBinary(a, b)
    res = s.add(a, b)
    print(res)
